//
//  SuperToursimdetailVC.m
//  Buiom_7.0
//
//  Created by lanou3g on 15/11/3.
//  Copyright © 2015年 triffe. All rights reserved.
//

#import "SuperToursimdetailVC.h"
#import "SuperToursimDetailModel.h"
#import "SuperToursimLastModel.h"
#import "SDCycleScrollView.h"

#import "SuperToursimDetailCell.h"

@interface SuperToursimdetailVC ()<UITableViewDataSource,UITableViewDelegate,MONActivityIndicatorViewDelegate>

@property (nonatomic, strong) NSMutableArray *imageArray;
// 装第一个model的数组
@property (nonatomic,strong) NSMutableArray *dataArray;
// 装第二个model的数组
@property (nonatomic, strong) NSMutableArray *otherArray;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSString *imageURL;

@property (nonatomic, assign) CGFloat heights;

// 设置一个图片的属性
@property (nonatomic, strong) UIImageView *imags;

@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UILabel *lables;
@property (nonatomic, assign) BOOL isTime;

@property (nonatomic, strong) MONActivityIndicatorView *indicationView;

@end

@implementation SuperToursimdetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg 2"]];
    [self addSubViews];
    // 隐藏tableview的分割线
    self.tableView.separatorStyle = NO;
    // 加载一个等待动画
    
    self.indicationView = [[MONActivityIndicatorView alloc] init];
    self.indicationView.center = self.tableView.center;
    CGPoint point = self.indicationView.center;
    point.y = self.tableView.center.y;
    self.indicationView.center = point;
    self.indicationView.delegate = self;
    [self.tableView addSubview:self.indicationView];
    [self.indicationView startAnimating];
    
    [self UpData];
    self.isTime = NO;
}

// 实现等待图片的加载动画的代理方法
- (UIColor *)activityIndicatorView:(MONActivityIndicatorView *)activityIndicatorView circleBackgroundColorAtIndex:(NSUInteger)index
{
    if (index == 0) {
        return [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
    } if (index == 1) {
        return [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
    } if (index == 2) {
        return [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
    } if (index == 3) {
        return [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
    }
    
    return [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
}



// 加载视图
- (void)addSubViews
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,  200, KsWidth, KsHeigh - 200 ) style:(UITableViewStyleGrouped)];
    self.tableView.delegate  = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:self.tableView];
    // 右边的tabBarbutton
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"share"] style:(UIBarButtonItemStylePlain) target:self action:@selector(rightItemAction)];
    self.navigationItem.rightBarButtonItem = rightBarButton;
    
    
}
// 设置按钮的登录和收藏的相关触发事件
- (void)rightItemAction
{
    
}


// 表头的高度
-  (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        
        return @"指南";
    }
   return @"更多资讯";
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        
        return 1;
    }
    
    return self.otherArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"sfggfesfdff";
    SuperToursimDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[SuperToursimDetailCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
    }
    
    if (indexPath.section == 0) {
        SuperToursimLastModel *model = self.dataArray[indexPath.row];
        cell.label1.text = [NSString stringWithFormat:@"城市名:       %@", model.cityname];
        cell.label2.text = [NSString stringWithFormat:@"开放时间:       %@", model.opentime];
        cell.label3.text = [NSString stringWithFormat:@"票价:        %ld", model.price];
        cell.label4.text = [NSString stringWithFormat:@"景点名:      %@", model.name];
        self.navigationItem.title = model.name;
        cell.label5.text = [NSString stringWithFormat:@"描述:  %@", model.descriptions];
        NSDictionary *dict = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:16] forKey:NSFontAttributeName];
        CGRect frame = [cell.label5.text boundingRectWithSize:CGSizeMake(KsWidth, 10000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dict context:nil];
        CGRect newFrame = cell.label5.frame;
        newFrame.size.height = frame.size.height;
        cell.label5.frame = newFrame;
        // 设置下面的显示框的frame
        CGRect newFrame1 = cell.label6.frame;
        newFrame1.origin.y = cell.label5.frame.origin.y + cell.label5.frame.size.height + 10;
        cell.label6.frame = newFrame1;

        cell.label6.text = [NSString stringWithFormat:@"地址:      %@", model.address];
        self.heights = cell.label6.bottom;
        
    } else {
    
    SuperToursimDetailModel *models = self.otherArray[indexPath.row];
        UIImageView *imageVies = [[UIImageView alloc] initWithFrame:CGRectMake(330, 0, 30, 40)];
        imageVies.image = [UIImage imageNamed:@"jump.jpg"];
        [cell.textLabel addSubview:imageVies];
        cell.textLabel.text = [NSString stringWithFormat:@"%@: %@", models.name, models.content];
   
    }
    // 加载出数据后,停止加载动画
    [self.indicationView stopAnimating];
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;

    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        return 40;
    }
    return self.heights;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 实现一个功能,让它显示点击行的内容
    if (indexPath.section == 1) {
        self.tableView.userInteractionEnabled = NO;
       SuperToursimDetailModel *models = self.otherArray[indexPath.row];
        // 定义一个视图
        self.imags = [[UIImageView alloc] initWithFrame:CGRectMake(0, KsHeigh, 1, 1)];
        self.imags.userInteractionEnabled = YES;
        // self.imags.backgroundColor = [UIColor redColor];
        self.imags.image = [UIImage imageNamed:@"bg"];
        self.lables = [[UILabel alloc] initWithFrame:CGRectMake(0, KsHeigh, 0, 0)];
        // labels.backgroundColor = [UIColor greenColor];
        self.lables.numberOfLines = 0;
        self.lables.font = [UIFont systemFontOfSize:16];
        self.lables.text = [NSString stringWithFormat:@"%@: %@", models.name, models.content];
        
        
        
        NSDictionary *dict = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:16] forKey:NSFontAttributeName];
        CGRect frame = [self.lables.text boundingRectWithSize:CGSizeMake(KsWidth, 10000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dict context:nil];
        CGRect newFrame = self.lables.frame;
        newFrame.size.height = frame.size.height;
        
        
        CGRect imageFrame = self.imags.frame;
        imageFrame.size.height = newFrame.size.height + 30;
        imageFrame.origin.y = KsHeigh - imageFrame.size.height - 5;
     
        [UIView animateWithDuration:1 animations:^{
      
            self.imags.frame = CGRectMake(0, KsHeigh - imageFrame.size.height - 10, KsWidth, imageFrame.size.height + 10);
            
            self.lables.frame = CGRectMake(5, KsHeigh - imageFrame.size.height - 2, KsWidth, newFrame.size.height);
                            }];
        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(actionTimer:) userInfo:nil repeats:YES];
        
        [timer fire];
        
        self.imags.image = [UIImage imageNamed:@"bg"];
        
        
        [self.view addSubview:_imags];
        [self.view addSubview:self.lables];
        
        
    }
}

- (void)actionTimer:(NSTimer *)timer
{
    if (self.isTime == NO) {
        self.isTime = !self.isTime;
        return;
    }
    self.button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.button.frame  = CGRectMake(0, KsHeigh - 30, KsWidth, 30);
    [self.button addTarget:self action:@selector(actionButtonSuper:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.button setTitle:@"确定" forState:(UIControlStateNormal)];
    self.button.backgroundColor = [UIColor purpleColor];

    
    [self.view addSubview:self.button];
    self.isTime = NO;
    [timer invalidate];
}

// 回复原来的大小
- (void)actionButtonSuper:(UIButton *)button
{
    self.tableView.userInteractionEnabled = YES;
    [UIView animateWithDuration:1 animations:^{
        // 执行的动画
        //            self.imags.center = CGPointMake(KsWidth / 2, KsHeigh - self.imags.height / 2);
        [self.button removeFromSuperview];
        self.lables.frame = CGRectMake(0, KsHeigh, 0, 0);
        self.imags.frame = CGRectMake(0, KsHeigh, 0, 0);
    }];

    
    
}

- (void)UpData
{
    NSURL *url = [NSURL URLWithString:@"http://www.roadqu.com/api/mobile/qunawan/poi/detail"];
    [self.dataArray removeAllObjects];
    [self.otherArray removeAllObjects];
    [self.imageArray removeAllObjects];
    //创建request网络请求对象
    NSMutableURLRequest *mutableURLRequest = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式
    [mutableURLRequest setHTTPMethod:@"post"];
    NSLog(@"##########%@", self.IDS);
    //设置请求参数
    NSData *postData = [[NSString stringWithFormat:@"&a_t=1437546765000&token=55a60422f1df1&code=%@&sign=b59f92e700f4ea76eeb287885586b93b7a764511&a_t=1437534035000&token=55a60422f1df1&mtid=%@&startpos=0&eachpage=5&sign=0050e3a746ed8a3afebffad573ea601ef24eeeea",self.code,self.IDS]dataUsingEncoding:NSUTF8StringEncoding];
    [mutableURLRequest setHTTPBody:postData];
    
    [NSURLConnection sendAsynchronousRequest:mutableURLRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        // 进行数据解析
        self.dataArray = [NSMutableArray array];
        self.imageArray = [NSMutableArray array];
        self.otherArray = [NSMutableArray array];
        
        if (data == nil) {
            return;
        }
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dicts = dic[@"data"];
        
        
            SuperToursimLastModel *model = [[SuperToursimLastModel alloc] init];
            [model setValuesForKeysWithDictionary:dicts];
        [self.dataArray addObject:model];
        
        NSArray *array = dicts[@"ext"];
        if (array.count == 0) {
            SuperToursimDetailModel *models = [[SuperToursimDetailModel alloc] init];
            models.name = @"十分抱歉";
            models.content = @"暂时没有更多的资讯,敬请期待!";
            [self.otherArray addObject:models];
        } else {
        for (NSDictionary *dic3 in array) {
                SuperToursimDetailModel *models = [[SuperToursimDetailModel alloc] init];
                [models setValuesForKeysWithDictionary:dic3];
                [self.otherArray addObject:models];
               }
           }
        
        NSArray *array2 = dicts[@"impress"];
        if (array2.count == 0) {
            [self.imageArray addObject:self.imageURLs];
        } else {
        for (NSDictionary *dic6 in array2) {
            self.imageURL = dic6[@"url"];
            [self.imageArray addObject:self.imageURL];
                }
            }
        // 表的创建
        NSArray *array3 = (NSArray *)self.imageArray;
        SDCycleScrollView *scrollViews  = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 64, KsWidth, 200 )imageURLStringsGroup:array3];
        [self.view addSubview:scrollViews];
        
        [self.tableView reloadData];
        
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
